package com.oocl.im.main;

import com.oocl.im.handle.IHandle;
import com.oocl.im.entity.Protocol;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.oocl.im.parser.IParser;
import com.oocl.im.parser.impl.Parser;
import com.oocl.im.constants.ClientMaintenance;

public class ClientRunnable implements Runnable {

	private ObjectOutputStream oos;
	private ObjectInputStream ois;

	public ClientRunnable() {
		super();
		prepare();
	}

	public void prepare() {

	}

	@Override
	public void run() {
		try {
			ois = ClientMaintenance.ois;
			oos = ClientMaintenance.oos;
			while (true) {
				Protocol protocol = (Protocol) ois.readObject();
				IParser parser = new Parser(oos);
				IHandle handle = parser.parse(protocol);
				if (handle != null) {
					handle.handle(protocol);
				}
			}
			
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
